package com.hymjweb.demo.hymj.basic_data_manage.xt_city.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.mapper.XtCityMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XtCityService extends BaseService {

    @Autowired
    private XtCityMapper xtCityMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtCityMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    public List<XtCityCachePojo> findCacheAll(Map map) {
        List<XtCityCachePojo> pojos = xtCityMapper.findCacheAll(map);
        return pojos;
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "城市-新增", operMemo = "", classType = XtCityPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtCityPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("城市新增参数错误");
        }
        checkPojo(pojo);
        Date currentDate = this.getSysTime();
        String czyId = this.getLoginCzyId();

        pojo.setCreateTime(currentDate);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentDate);
        pojo.setUpdateUserId(czyId);

        int affectRows = xtCityMapper.insert(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtCityCache();
        }
        return affectRows;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle = "城市-更新", operMemo = "", classType = XtCityPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtCityPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("城市更新参数错误");
        }
        checkPojo(pojo);
        pojo.setUpdateTime(this.getSysTime());
        pojo.setUpdateUserId(this.getLoginCzyId());
        int flag = xtCityMapper.update(pojo);
        if (flag > SysConstant.INT_ZERO) {
            globalCacheService.loadXtCityCache();
        }
        return flag;
    }

    /**
     * 检查必输项
     *
     * @param pojo
     */
    private void checkPojo(XtCityPojo pojo) throws Exception {
        DataTypeUtil.isEmptyStrThenThrow(pojo.getId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getName(), SysConstant.XT_DIC_ERROR_MSG.no_name.getName());
    }

    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle = "城市-删除", operMemo = "", classType = String.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throw new Exception("城市删除参数错误");
        }
        int affectRows = xtCityMapper.delete(id);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtCityCache();
        }
        return affectRows;
    }


    public XtCityPojo fetch(String cityId) {
        if (DataTypeUtil.isEmptyStr(cityId)) {
            return null;
        }
        return xtCityMapper.fetch(cityId);
    }
}
